View Javadoc

1   // Atomic.java, created Mon Feb  5 23:23:21 2001 by joewhaley
2   // Copyright (C) 2001-3 John Whaley <jwhaley@alum.mit.edu>
3   // Licensed under the terms of the GNU LGPL; see COPYING for details.
4   package joeq.Synchronization;
5   
6   import java.lang.reflect.Field;
7   import java.lang.reflect.Modifier;
8   import joeq.Class.jq_InstanceField;
9   import joeq.Main.jq;
10  import joeq.Memory.HeapAddress;
11  import joeq.Runtime.Reflection;
12  import jwutil.util.Assert;
13  
14  /*
15   * @author  John Whaley <jwhaley@alum.mit.edu>
16   * @version $Id: Atomic.java 1941 2004-09-30 03:37:06Z joewhaley $
17   */
18  public abstract class Atomic {
19      
20      public static final int cas4(Object o, jq_InstanceField f, int before, int after) {
21          if (!jq.RunningNative) {
22              Field f2 = (Field)Reflection.getJDKMember(f);
23              f2.setAccessible(true);
24              Assert._assert((f2.getModifiers() & Modifier.STATIC) == 0);
25              try {
26                  int v = ((Integer)f2.get(o)).intValue();
27                  if (v == before) {
28                      f2.set(o, new Integer(after));
29                      return after;
30                  } else {
31                      return v;
32                  }
33              } catch (IllegalAccessException x) {
34                  Assert.UNREACHABLE();
35                  return 0;
36              }
37          } else {
38              HeapAddress address = (HeapAddress) HeapAddress.addressOf(o).offset(f.getOffset());
39              return address.atomicCas4(before, after);
40          }
41      }
42      
43      public static final long cas8(Object o, jq_InstanceField f, long before, long after) {
44          if (!jq.RunningNative) {
45              Field f2 = (Field)Reflection.getJDKMember(f);
46              f2.setAccessible(true);
47              Assert._assert((f2.getModifiers() & Modifier.STATIC) == 0);
48              try {
49                  long v = ((Long)f2.get(o)).intValue();
50                  if (v == before) {
51                      f2.set(o, new Long(after));
52                      return after;
53                  } else {
54                      return v;
55                  }
56              } catch (IllegalAccessException x) {
57                  Assert.UNREACHABLE();
58                  return 0;
59              }
60          } else {
61              HeapAddress address = (HeapAddress) HeapAddress.addressOf(o).offset(f.getOffset());
62              return address.atomicCas8(before, after);
63          }
64      }
65  }